package com.demo.crud.util;

import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: keen
 * Date: 2019-06-05
 * Time: 22:06
 */
public final class MD5Utils {
    public static String encode(String key) {
        try {
            if (StringUtils.isEmpty(key)) {
                throw new IllegalArgumentException("key不能为空");
            }
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] bytes = digest.digest(key.getBytes());
            return new BASE64Encoder().encode(bytes);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}
